﻿; (function (global) {

    'use strict';

    global.GridHintsDrawer = function GridHintsDrawer(uiGrid) {
        this.uiGrid = uiGrid;
        this.hintedCandidates = [];
        EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchHintResult, this.onHintsResult.bind(this));
        EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchHintClear, this.onHintsClear.bind(this));
    }
    GridHintsDrawer.prototype.onHintsClear = function (ev, result) {
        this.clearHints();
    }

    GridHintsDrawer.prototype.addHintedCandidates = function (candidates) {
        for (var i = 0; i < candidates.length; i++) {
            this.addHintedCandidate(candidates[i]);
        }
    }

    GridHintsDrawer.prototype.addHintedCandidate = function (candidate) {
        if (!this.hintedCandidates.contains(candidate))
            this.hintedCandidates.push(candidate);
    }

    GridHintsDrawer.prototype.clearHints = function () {
        for (var i = 0; i < this.hintedCandidates.length; i++) {
            var candidate = this.hintedCandidates[i];
            candidate.classList.remove("hint-ok");
            candidate.classList.remove("hint-remove");
            candidate.classList.remove("hint-on");
            candidate.classList.remove("hint-off");
            candidate.classList.remove("hint-conflict");
            candidate.classList.remove("hint-seetwo");
        }
    }

    GridHintsDrawer.prototype.onHintsResult = function (ev, result) {
        this.clearHints();

        if (!result) {
            return;
        }

        if (result.strategy instanceof Strategies.NakedSingle) {
            var cellModel = result.cell;
            var candidate = this.uiGrid.findCandidate(cellModel.indices, result.candidate);
            $(candidate).addClass("hint-ok");

            this.addHintedCandidate(candidate);
            this.uiGrid.onGridFlashGlowCells(ev, [cellModel.indices]);
            return;
        }

        if (result.strategy instanceof Strategies.HiddenSingle) {
            var cellModel = result.cell;
            var candidate = this.uiGrid.findCandidate(cellModel.indices, result.candidate);
            $(candidate).addClass("hint-ok");
            this.addHintedCandidate(candidate);

            this.uiGrid.onGridFlashGlowCells(ev, result.relatedCells.map(function (o) { return o.indices; }));
            return;
        }

        if (result.strategy instanceof Strategies.PointingPair) {
            var containingCells = result.containingCells;
            var candidates = this.uiGrid.findCandidateInCells(containingCells.map(function (o) { return o.indices; }), result.candidate);
            $(candidates).addClass("hint-ok");
            this.addHintedCandidates(candidates);

            var missingCandidates = this.uiGrid.findCandidateInCells(result.missingCandidatesCellsIndices, result.candidate);
            $(missingCandidates).addClass("hint-remove");
            this.addHintedCandidates(missingCandidates);

            this.uiGrid.onGridFlashGlowCells(ev, result.relatedCells.map(function (o) { return o.indices; }));
            return;
        }

        if (result.strategy instanceof Strategies.PointingInside) {
            var containingCells = result.containingCells;
            var candidates = this.uiGrid.findCandidateInCells(containingCells.map(function (o) { return o.indices; }), result.candidate);
            $(candidates).addClass("hint-ok");
            this.addHintedCandidates(candidates);

            var missingCandidates = this.uiGrid.findCandidateInCells(result.missingCandidatesCellsIndices, result.candidate);
            $(missingCandidates).addClass("hint-remove");
            this.addHintedCandidates(missingCandidates);


            this.uiGrid.onGridFlashGlowCells(ev, result.relatedCells.map(function (o) { return o.indices; }));
            return;
        }

        if (result.strategy instanceof Strategies.NakedPair) {
            var containingCells = result.containingCells;
            var candidates = this.uiGrid.findCandidatesInCells(containingCells.map(function (o) { return o.indices; }), result.candidates);
            $(candidates).addClass("hint-ok");
            this.addHintedCandidates(candidates);

            var missingCandidates = this.uiGrid.findCandidatesInCells(result.missingCandidatesCellsIndices, result.candidates);
            $(missingCandidates).addClass("hint-remove");
            this.addHintedCandidates(missingCandidates);

            this.uiGrid.onGridFlashGlowCells(ev, result.relatedCells.map(function (o) { return o.indices; }));
            return;
        }

        if (result.strategy instanceof Strategies.Burma) {
            var containingCells = result.containingCells;
            var candidates = this.uiGrid.findCandidateInCells(containingCells.map(function (o) { return o.indices; }), result.candidate);
            $(candidates).addClass("hint-ok");
            this.addHintedCandidates(candidates);

            var missingCandidates = this.uiGrid.findCandidateInCells(result.missingCandidatesCellsIndices, result.candidate);
            $(missingCandidates).addClass("hint-remove");
            this.addHintedCandidates(missingCandidates);

            this.uiGrid.onGridFlashGlowCells(ev, result.relatedCells.map(function (o) { return o.indices; }));
            return;
        }

        if (result.strategy instanceof Strategies.SimpleColoring) {


            for (var i = 0; i < result.chains.length; i++) {
                var chain = result.chains[i];
                var candidates = this.uiGrid.findCandidateInCells(chain.on.map(function (o) { return o.indices; }), result.candidate);
                $(candidates).addClass("hint-on");
                this.addHintedCandidates(candidates);

                candidates = this.uiGrid.findCandidateInCells(chain.off.map(function (o) { return o.indices; }), result.candidate);
                $(candidates).addClass("hint-off");
                this.addHintedCandidates(candidates);
            }

            var conflictCandidates = this.uiGrid.findCandidateInCells(result.cellsConflict.map(function (o) { return o.indices; }), result.candidate);
            $(conflictCandidates).addClass("hint-conflict");
            this.addHintedCandidates(conflictCandidates);

            var missingCandidates = this.uiGrid.findCandidateInCells(result.cellsSeeTwo.map(function (o) { return o.indices; }), result.candidate);
            $(missingCandidates).addClass("hint-seetwo");
            this.addHintedCandidates(missingCandidates);

            this.uiGrid.onGridFlashGlowCells(ev, result.relatedCells.map(function (o) { return o.indices; }));
            return;
        }
    }


}(this));
